package com.shuidi.oilactivity.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.shuidi.oilactivity.R;
import com.shuidi.oilactivity.bean.Customer;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

public class AddCustomerActivity extends BaseActivity {

    @BindView(R.id.top_left_btn)
    ImageButton topLeftBtn;
    @BindView(R.id.title_bar)
    TextView titleBar;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.man)
    TextView man;
    @BindView(R.id.woman)
    TextView woman;
    @BindView(R.id.layout_sex)
    LinearLayout layoutSex;
    @BindView(R.id.name)
    EditText name;
    @BindView(R.id.layout_name)
    LinearLayout layoutName;
    @BindView(R.id.iphone)
    EditText iphone;
    @BindView(R.id.layout_iphone)
    LinearLayout layoutIphone;
    @BindView(R.id.tv_user)
    TextView tvUser;
    @BindView(R.id.layout_user)
    RelativeLayout layoutUser;
    @BindView(R.id.layout)
    LinearLayout layout;
    @BindView(R.id.tv_oil)
    TextView tvOil;
    @BindView(R.id.layout_oil)
    RelativeLayout layoutOil;
    @BindView(R.id.layout_month)
    LinearLayout layoutMonth;
    @BindView(R.id.layout_cycle)
    LinearLayout layoutCycle;
    @BindView(R.id.tv_want)
    TextView tvWant;
    @BindView(R.id.layout_want)
    RelativeLayout layoutWant;
    @BindView(R.id.go)
    Button go;
    @BindView(R.id.cycle)
    EditText cycle;
    @BindView(R.id.user)
    TextView user;
    @BindView(R.id.month)
    EditText month;
    @BindView(R.id.want)
    TextView want;
    @BindView(R.id.oil)
    TextView oil;
    @BindView(R.id.small)
    TextView small;
    @BindView(R.id.big)
    TextView big;
    @BindView(R.id.user_type)
    LinearLayout userType;

    private int sex = 1;
    private int userTypeId = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_customer);
        ButterKnife.bind(this);
        titleBar.setText("添加新客户");
    }

    @OnClick({R.id.top_left_btn, R.id.man, R.id.woman, R.id.layout_user, R.id.layout_oil, R.id.layout_want, R.id.go,R.id.small, R.id.big})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.top_left_btn:
                finish();
                break;
            case R.id.man:
                sex = 1;
                man.setTextColor(Color.parseColor("#ffffff"));
                man.setBackgroundResource(R.drawable.left_sex_yes);
                woman.setTextColor(Color.parseColor("#84C1FF"));
                woman.setBackgroundResource(R.drawable.right_sex_no);
                break;
            case R.id.woman:
                sex = 2;
                man.setTextColor(Color.parseColor("#84C1FF"));
                man.setBackgroundResource(R.drawable.left_sex_no);
                woman.setTextColor(Color.parseColor("#ffffff"));
                woman.setBackgroundResource(R.drawable.right_sex_yes);
                break;
            case R.id.layout_user:
                InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
                }

                View viewUser = LayoutInflater.from(this).inflate(R.layout.source_dialog, null);

                TextView web = (TextView) viewUser.findViewById(R.id.web);
                TextView visit = (TextView) viewUser.findViewById(R.id.visit);
                TextView company = (TextView) viewUser.findViewById(R.id.company);
                TextView customer = (TextView) viewUser.findViewById(R.id.customer);
                TextView develop = (TextView) viewUser.findViewById(R.id.develop);
                TextView cancel = (TextView) viewUser.findViewById(R.id.cancel);

                final AlertDialog alertDialog = new AlertDialog.Builder(this, R.style.DialogStyle)
                        .setView(viewUser)
                        .create();
                alertDialog.show();

                web.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        user.setText("网站注册");
                        alertDialog.dismiss();
                    }
                });

                visit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        user.setText("地推拜访");
                        alertDialog.dismiss();
                    }
                });

                company.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        user.setText("公司发放");
                        alertDialog.dismiss();
                    }
                });

                customer.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        user.setText("客户推荐");
                        alertDialog.dismiss();
                    }
                });

                develop.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        user.setText("电话或互联网开发");
                        alertDialog.dismiss();
                    }
                });

                cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        alertDialog.dismiss();
                    }
                });

                Window window = alertDialog.getWindow();
                window.setGravity(Gravity.BOTTOM);
                window.setWindowAnimations(R.style.popupAnimation);
                alertDialog.setCanceledOnTouchOutside(true);
                alertDialog.setCancelable(true);

                WindowManager m = this.getWindowManager();
                Display d = m.getDefaultDisplay(); // 获取屏幕宽、高度
                WindowManager.LayoutParams p = window.getAttributes(); // 获取对话框当前的参数值   
                p.width = (int) (d.getWidth() * 0.93); // 宽度设置为屏幕的0.93，根据实际情况调整
                window.setAttributes(p);

                break;
            case R.id.layout_oil:
                InputMethodManager imm2 = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                if (imm2 != null) {
                    imm2.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
                }
                View viewOil = LayoutInflater.from(this).inflate(R.layout.oil_type_dialog, null);
                TextView ordinary = (TextView) viewOil.findViewById(R.id.ordinary);
                TextView five = (TextView) viewOil.findViewById(R.id.five);
                TextView oilCancel = (TextView) viewOil.findViewById(R.id.cancel);

                final AlertDialog oilAlertDialog = new AlertDialog.Builder(this, R.style.DialogStyle)
                        .setView(viewOil)
                        .create();
                oilAlertDialog.show();

                ordinary.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        oil.setText("普柴");
                        oilAlertDialog.dismiss();
                    }
                });

                five.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        oil.setText("国五");
                        oilAlertDialog.dismiss();
                    }
                });

                oilCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        oilAlertDialog.dismiss();
                    }
                });

                Window oilWindow = oilAlertDialog.getWindow();
                oilWindow.setGravity(Gravity.BOTTOM);
                oilWindow.setWindowAnimations(R.style.popupAnimation);
                oilAlertDialog.setCanceledOnTouchOutside(true);
                oilAlertDialog.setCancelable(true);

                WindowManager oilm = this.getWindowManager();
                Display oild = oilm.getDefaultDisplay(); // 获取屏幕宽、高度
                WindowManager.LayoutParams oilp = oilWindow.getAttributes(); // 获取对话框当前的参数值   
                oilp.width = (int) (oild.getWidth() * 0.93); // 宽度设置为屏幕的0.93，根据实际情况调整
                oilWindow.setAttributes(oilp);

                break;
            case R.id.layout_want:
                InputMethodManager imm3 = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                if (imm3 != null) {
                    imm3.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
                }
                View viewWant = LayoutInflater.from(this).inflate(R.layout.want_dialog, null);
                TextView very = (TextView) viewWant.findViewById(R.id.very);
                TextView think = (TextView) viewWant.findViewById(R.id.want);
                TextView normal = (TextView) viewWant.findViewById(R.id.normal);
                TextView noThink = (TextView) viewWant.findViewById(R.id.no_think);
                TextView wantCancel = (TextView) viewWant.findViewById(R.id.cancel);

                final AlertDialog wantAlertDialog = new AlertDialog.Builder(this, R.style.DialogStyle)
                        .setView(viewWant)
                        .create();
                wantAlertDialog.show();

                very.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        want.setText("强烈");
                        wantAlertDialog.dismiss();
                    }
                });

                think.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        want.setText("感兴趣");
                        wantAlertDialog.dismiss();
                    }
                });

                normal.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        want.setText("普通");
                        wantAlertDialog.dismiss();
                    }
                });

                noThink.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        want.setText("不确定");
                        wantAlertDialog.dismiss();
                    }
                });

                wantCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        wantAlertDialog.dismiss();
                    }
                });

                Window wantWindow = wantAlertDialog.getWindow();
                wantWindow.setGravity(Gravity.BOTTOM);
                wantWindow.setWindowAnimations(R.style.popupAnimation);
                wantAlertDialog.setCanceledOnTouchOutside(true);
                wantAlertDialog.setCancelable(true);

                WindowManager wantm = this.getWindowManager();
                Display wantd = wantm.getDefaultDisplay(); // 获取屏幕宽、高度
                WindowManager.LayoutParams wantp = wantWindow.getAttributes(); // 获取对话框当前的参数值   
                wantp.width = (int) (wantd.getWidth() * 0.93); // 宽度设置为屏幕的0.93，根据实际情况调整
                wantWindow.setAttributes(wantp);

                break;
            case R.id.small:

                userTypeId = 1;
                small.setTextColor(Color.parseColor("#ffffff"));
                small.setBackgroundResource(R.drawable.left_sex_yes);
                big.setTextColor(Color.parseColor("#84C1FF"));
                big.setBackgroundResource(R.drawable.right_sex_no);
                break;
            case R.id.big:
                userTypeId = 2;
                small.setTextColor(Color.parseColor("#84C1FF"));
                small.setBackgroundResource(R.drawable.left_sex_no);
                big.setTextColor(Color.parseColor("#ffffff"));
                big.setBackgroundResource(R.drawable.right_sex_yes);
                break;
            case R.id.go:

                if (name.getText().toString().length() == 0) {
                    Toast.makeText(this, "请填写客户姓名", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (iphone.getText().toString().length() == 0) {
                    Toast.makeText(this, "请填写手机号码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!iphone.getText().toString().substring(0, 1).equals("1")) {
                    Toast.makeText(this, "手机号码格式错误", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (user.getText().toString().length() == 0) {
                    Toast.makeText(this, "请选择用户来源", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (oil.getText().toString().length() == 0) {
                    Toast.makeText(this, "请选择用油类型", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (month.getText().toString().length() == 0) {
                    Toast.makeText(this, "请填写月用量(升)", Toast.LENGTH_SHORT).show();
                }
                if (cycle.getText().toString().length() == 0) {
                    Toast.makeText(this, "请填写采购周期", Toast.LENGTH_SHORT).show();
                }
                if (want.getText().toString().length() == 0) {
                    Toast.makeText(this, "请选择购买意愿", Toast.LENGTH_SHORT).show();
                    return;
                }

                addData();
                break;
        }
    }

    private void addData() {
        Customer customerBean = new Customer();
        customerBean.setName(name.getText().toString());
        customerBean.setSex(sex);
        customerBean.setIphone(iphone.getText().toString());
        customerBean.setSource(user.getText().toString());
        customerBean.setOilType(oil.getText().toString());
        customerBean.setMonthUse(month.getText().toString());
        customerBean.setCycle(cycle.getText().toString());
        customerBean.setWant(want.getText().toString());
        customerBean.setUserType(userTypeId);
        customerBean.save(new SaveListener<String>() {
            @Override
            public void done(String objectId, BmobException e) {
                if (e == null) {
                    Toast.makeText(AddCustomerActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
                    CustomerActivity.customerActivity.allCustomerNumber();
                    finish();
                } else {
                    Toast.makeText(AddCustomerActivity.this, "添加失败，请联系管理员", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
